package com.sd.repay.service.impl;

import com.sd.repay.common.Constants;
import com.sd.repay.dao.BaseDao;
import com.sd.repay.pojo.Balance;
import com.sd.repay.pojo.BalanceHis;
import com.sd.repay.service.BalanceOperNotTranService;
import com.sd.repay.utils.DES3Coder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by sd on 2018/08/31.
 * <p>
 * 非事务方式运行余额操作服务
 */
@Service
public class BalanceOperNotTranServiceImpl implements BalanceOperNotTranService {

    private static Logger log = LoggerFactory.getLogger(BalanceOperNotTranServiceImpl.class);

    @Autowired
    private BaseDao baseDao;

    /**
     * 新增余额账户
     *
     * @param insertBalance
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public int insertBalanceNotTransactional(Balance insertBalance) throws Exception {

        String merType = insertBalance.getMerType();
        String merNo = insertBalance.getMerNo();
        String currBalance = "0.00";
        String hisBalance = "0.00";
        String freezeBalance = "0.00";

        String encryptTotalBalance = DES3Coder.encryptToString(merType + merNo + currBalance);

        String insertSql = " INSERT INTO t_balance (balance_no,mer_type,mer_no,balance_channel,curr_balance,his_balance,freeze_balance," +
                "encrypt_total_balance,status,create_time) VALUES (?,?,?,?,?,?,?,?,?,?) ";

        List<Object> params = new ArrayList<>();
        params.add(insertBalance.getBalanceNo());
        params.add(merType);
        params.add(merNo);
        params.add(insertBalance.getBalanceChannel());
        params.add(currBalance);
        params.add(hisBalance);
        params.add(freezeBalance);
        params.add(encryptTotalBalance);
        params.add(Constants.BALANCE_STATUS_SUCCESS);
        params.add(new Date());

        return baseDao.execute(insertSql, params.toArray());
    }

    /**
     * 新增余额流水
     *
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public int insertBalanceHisNotTransactional(BalanceHis insertBalanceHis) {

        String insertSql = " INSERT INTO t_balance_his (his_no,balance_no,mer_type,mer_no,amount_front,amount,amount_behind,trans_type,balance_type," +
                " service,service_order_no,result_status,result_msg,remark,create_time) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";

        List<Object> list = new ArrayList<>();
        list.add(insertBalanceHis.getHisNo());
        list.add(insertBalanceHis.getBalanceNo());
        list.add(insertBalanceHis.getMerType());
        list.add(insertBalanceHis.getMerNo());
        list.add(insertBalanceHis.getAmountFront());
        list.add(insertBalanceHis.getAmount());
        list.add(insertBalanceHis.getAmountBehind());
        list.add(insertBalanceHis.getTransType());
        list.add(insertBalanceHis.getBalanceType());
        list.add(insertBalanceHis.getService());
        list.add(insertBalanceHis.getServiceOrderNo());
        list.add(insertBalanceHis.getResultStatus());
        list.add(insertBalanceHis.getResultMsg());
        list.add(insertBalanceHis.getRemark());
        list.add(new Date());
        return baseDao.execute(insertSql, list.toArray());
    }
}
